<?php

class PostsModel extends Posts{
	
	public function insertPosts($arr){
		try {
			$ar_d 		 = explode('/',$arr['data']);
			$arr['data'] = $ar_d[2].'-'.$ar_d[1].'-'.$ar_d[0];
			$obj = new Posts();			
			foreach($arr as $chave => $valor){
				if($chave != 'files'){
					$chave = ucfirst($chave);
					$met = 'set'.$chave;
					$obj->$met($valor);
				}
			}
			$db = new Db;
			$db->Insert('posts',$obj);
			$this->lista = $db->res;
		}catch (Exception $e) {
			echo 'Erro: ',  $e->getMessage();
		}
	}
	public function updatePosts($arr){
		try {
			$ar_d 		 = explode('/',$arr['data']);
			$arr['data'] = $ar_d[2].'-'.$ar_d[1].'-'.$ar_d[0];
			$obj = new Posts();
			foreach($arr as $chave => $valor){
				if($chave != 'files'){
					$chave = ucfirst($chave);
					$met = 'set'.$chave;
					$valor = "'".$valor."'";
					$obj->$met($valor);
				}
			}
			$db = new Db;
			$db->Update('posts',$obj);
			$this->lista = $db->res;
		}catch (Exception $e) {
			echo 'Erro: ',  $e->getMessage();
		}
	}
	public function deletePosts($id){
		try {
			$db = new Db;
			$db->Delete('posts',$id);
			$this->lista = $db->res;
		} catch (Exception $e) {
			echo 'Erro: ',  $e->getMessage();
		}
	}
	public function consultarPosts($parametros){
		try {
			$campos = array('*');
			$where = 'p.idposts = '.$parametros;
			$order = '';
			$db = new Db;
			$db->Select('posts AS p INNER JOIN imagens AS i ON i.idimagens = p.idimagens',$campos,$where,$order);
			$this->lista = $db->res[0];
		}catch (Exception $e) {
			echo 'Erro: ',  $e->getMessage();
		}
	}
	public function dropdownPosts($parametros){
		try {
			$campos = array('*');
			$where = '';
			$order = 'data DESC';
			$db = new Db;
			$db->Select('posts',$campos,$where,$order);
			$view = new PostsView();
			$this->lista = $view->select($db->res);
		}catch (Exception $e) {
			echo 'Erro: ',  $e->getMessage();
		}
	}
	public function listarPosts($parametros){
		try {
			$campos = array('*');
			$where = '';
			$order = 'p.data DESC';
			$db = new Db;
			$db->Select('posts AS p INNER JOIN categorias AS c ON c.idcategorias = p.idcategorias',$campos,$where,$order);
			$view = new PostsView();
			$this->lista = $view->listar($db->res);
		}catch (Exception $e) {
			echo 'Erro: ',  $e->getMessage();
		}
	}
	public function tagsPosts($parametros){
		try {
			$campos = array('tags');
			$where = '';
			$order = 'tags ASC';
			$db = new Db;
			$db->Select('posts',$campos,$where,$order);
			$view = new PostsView();
			$this->lista = $view->tags($db->res);
		}catch (Exception $e) {
			echo 'Erro: ',  $e->getMessage();
		}
	}
	public function arquivoPosts($parametros){
		try {
			$campos = array('data');
			$where = '';
			$order = 'data DESC';
			$db = new Db;
			$db->Select('posts',$campos,$where,$order);
			$view = new PostsView();
			$this->lista = $view->arquivo($db->res);
		}catch (Exception $e) {
			echo 'Erro: ',  $e->getMessage();
		}
	}
	public function capaPosts($parametros){
		try {
			$campos = array('*');
			$where = '';
			$order = 'data DESC LIMIT 2';
			$db = new Db;
			$db->Select('posts AS p INNER JOIN imagens AS i ON i.idimagens = p.idimagens',$campos,$where,$order);
			$view = new PostsView();
			$this->lista = $view->capa($db->res);
		}catch (Exception $e) {
			echo 'Erro: ',  $e->getMessage();
		}
	}
	public function visualizarPosts($parametros){
		try {
			$campos = array('*');
			$where = '';
			if($parametros[0] == 'c'){
				$where .= 'p.idcategorias = '.$parametros[1].' AND ';
			}elseif($parametros[0] == 't'){
				$where .= 'p.tags LIKE "%'.$parametros[1].'%" AND ';
			}elseif($parametros[0] == 'a'){
				$dt = explode('-',$parametros[1]);
				$where .= 'p.data BETWEEN "'.$dt[1].'-'.$dt[0].'-00" AND "'.$dt[1].'-'.$dt[0].'-31" AND ';
			}elseif($parametros[0] == 's'){
				$where .= '(p.titulo LIKE "%'.$parametros[1].'%" OR p.texto LIKE "%'.$parametros[1].'%") AND ';
			}
			$where .= 'p.ativo = 1';
			$order = 'p.data DESC';
			$db = new Db;
			$db->Select('posts AS p INNER JOIN categorias AS c ON c.idcategorias = p.idcategorias',$campos,$where,$order);
			$view = new PostsView();
			$this->lista = $view->visualizar($db->res);
		}catch (Exception $e) {
			echo 'Erro: ',  $e->getMessage();
		}
	}
}
?>